/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.auth.utils.DateUtils;
import org.springblade.modules.demo.entity.DtmExpDataSum;
import org.springblade.modules.demo.service.HomePageProcessingService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.demo.entity.DtmExpCardStoreSum;
import org.springblade.modules.demo.vo.DtmExpCardStoreSumVO;
import org.springblade.modules.demo.wrapper.DtmExpCardStoreSumWrapper;
import org.springblade.modules.demo.service.IDtmExpCardStoreSumService;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 票卡使用商户统计 控制器
 *
 * @author BladeX
 * @since 2021-12-24
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blade-demo/dtmexpcardstoresum")
@Api(value = "票卡使用商户统计", tags = "票卡使用商户统计接口")
public class DtmExpCardStoreSumController extends BladeController {

	private final HomePageProcessingService homePageProcessingService;

	private final IDtmExpCardStoreSumService dtmExpCardStoreSumService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dtmExpCardStoreSum")
	public R<DtmExpCardStoreSumVO> detail(DtmExpCardStoreSum dtmExpCardStoreSum) {
		DtmExpCardStoreSum detail = dtmExpCardStoreSumService.getOne(Condition.getQueryWrapper(dtmExpCardStoreSum));
		return R.data(DtmExpCardStoreSumWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 票卡使用商户统计
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入dtmExpCardStoreSum")
	public R<IPage<DtmExpCardStoreSumVO>> list(DtmExpCardStoreSum dtmExpCardStoreSum, Query query) {
		IPage<DtmExpCardStoreSum> pages = dtmExpCardStoreSumService.page(Condition.getPage(query), Condition.getQueryWrapper(dtmExpCardStoreSum));
		return R.data(DtmExpCardStoreSumWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 票卡使用商户统计
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入dtmExpCardStoreSum")
	public R<IPage<DtmExpCardStoreSumVO>> page(DtmExpCardStoreSumVO dtmExpCardStoreSum, Query query) {
		IPage<DtmExpCardStoreSumVO> pages = dtmExpCardStoreSumService.selectDtmExpCardStoreSumPage(Condition.getPage(query), dtmExpCardStoreSum);
		return R.data(pages);
	}

	/**
	 * 新增 票卡使用商户统计
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入dtmExpCardStoreSum")
	public R save(@Valid @RequestBody DtmExpCardStoreSum dtmExpCardStoreSum) {
		return R.status(dtmExpCardStoreSumService.save(dtmExpCardStoreSum));
	}

	/**
	 * 修改 票卡使用商户统计
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入dtmExpCardStoreSum")
	public R update(@Valid @RequestBody DtmExpCardStoreSum dtmExpCardStoreSum) {
		return R.status(dtmExpCardStoreSumService.updateById(dtmExpCardStoreSum));
	}


	/**
	 * 新增或修改 票卡使用商户统计
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入dtmExpCardStoreSum")
	public R submit(@Valid @RequestBody DtmExpCardStoreSum dtmExpCardStoreSum) {
		return R.status(dtmExpCardStoreSumService.saveOrUpdate(dtmExpCardStoreSum));
	}


	/**
	 * 删除 票卡使用商户统计
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(dtmExpCardStoreSumService.deleteLogic(Func.toLongList(ids)));
	}


	/********************************************************/




	/**
	 * 卡码用户数量 累计和以及同比环比返回  list返回
	 * 測試接口
	 */
	@GetMapping("/list1")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入simp101001")
	public R list1(@ApiIgnore @RequestParam Map<String, Object> dtmExpDataSum, Query query)
	{
		//扩展 将其他的逻辑加进来 准备抽出来 放到首页的controller里面去
//		list.add(homePageProcessingService.tradeCountMap());
//		list.add(homePageProcessingService.realTicketPriceMap());
//		list.add(homePageProcessingService.unitPriceMap());
		log.info("测试响应接口list11");
		List<Map<String, Object>> list = new ArrayList<>();
		//如果前端要list 也可以
		list.add(homePageProcessingService.cardCountMap(0));
		R.data(list);
		return R.data(list);
	}








//	/**
//	 *返回传参 查询到的总和 其他过滤条件都是 服务商是0001, 清算时间是 2021-09-02(测试数据就这日期的)   			//TRADE_COUNT 总交易次数
//	 * SELECT sum(TRADE_COUNT) as sumAll FROM T_DTM_EXP_DATA_SUM
//	 *
//	 *
//	 * SELECT sum(TRADE_COUNT) as sumAll FROM T_DTM_EXP_DATA_SUM  WHERE LIQ_CODE='0001' and LIQ_DATE= '2021-09-02'
//	 *
//	 * */
//	public  int query1(String str){
//		DtmExpCardStoreSum dtmExpCardStoreSum=new DtmExpCardStoreSum();
//
//		QueryWrapper<DtmExpCardStoreSum> queryWrapper=new QueryWrapper<>();
//		//获取指定用户 参数2就是 具体的值  这里先写死一个
//		String data="sum("+ str + ") as sumAll";
//		//数据求和
//		queryWrapper.select(/*"sum(TRADE_COUNT) as sumAll"*/data);
//		//服务商 是0001的 实际应该是查询当前账户对应的服务商
//		//TODO
//		BladeUser bladeUser= AuthUtil.getUser();
//		log.info("测试响应接口角色信息："+bladeUser);
//		queryWrapper.eq(/*"liqCode"*/"LIQ_CODE","0001");
//		//清算日期是 2021-09-02  实际这里是服务器的当天时间
//		//TODO
//		String toDayDate = DateUtils.getDate() ;
//		log.info("测试响应接口toDayDate："+toDayDate);
//		queryWrapper.eq(/*"liqDate"*/"LIQ_DATE","2021-09-02"/*toDayDate*/);
////		//交易笔数
////		queryWrapper.eq("tradeCount","2021-09-02");
//		log.info("测试响应接口1：");
//		dtmExpCardStoreSum=dtmExpCardStoreSumService.getOne(queryWrapper);
//		Map<String,Object> map=dtmExpCardStoreSumService.getMap(queryWrapper);
//		log.info("测试响应接口2"+map);
//		int a=-1;
//		try{
//			a=	Integer.parseInt(map.get("SUMALL").toString());
//
//		}catch (NumberFormatException e){
//			e.printStackTrace();
//		}
//		return  a;
//	}
//
//
//
//	/**
//	 * 查询指定字段的累计和
//	 * 参数1:表里面的指定字段
//	 * 参数2: 查询的清算时间
//	 *
//	 * return:返回指定字段的 指定时间的交易次数累计和
//	 *
//	 * */
//	public  int query2(String str,String date){
//		DtmExpCardStoreSum dtmExpCardStoreSum=new DtmExpCardStoreSum();
//		QueryWrapper<DtmExpCardStoreSum> queryWrapper=new QueryWrapper<>();
//		//获取指定用户 参数2就是 具体的值  这里先写死一个
//		//数据求和
//		String data="sum("+ str /*"TRADE_COUNT"*/  +") as sumAll";
//		queryWrapper.select(data);
//		//服务商 是0001的 实际应该是查询当前账户对应的服务商
//		queryWrapper.eq(/*"liqCode"*/"LIQ_CODE","0001");
//		//清算日期是 2021-09-02  实际这里是服务器的当天时间
//		//TODO
//		queryWrapper.eq("LIQ_DATE",date);
////		//交易笔数
////		queryWrapper.eq("tradeCount","2021-09-02");
//		log.info("测试响应接口1：");
//		Map<String,Object> map=dtmExpCardStoreSumService.getMap(queryWrapper);
//		log.info("测试响应接口2"+map);
//		//查询过滤后的值为null   比如查询对比的某个日期的数据 在数据库不存在
//		if (map==null)
//		{
//			log.info("测试响应接口2.1："+map);
//			return  -1;
//		}
//		int a=-1;
//		try{
//			a=	Integer.parseInt(map.get("SUMALL").toString());
//
//		}catch (NumberFormatException e){
//			e.printStackTrace();
//		}
//		return  a;
//	}
//
//
//	/**
//	 * 同比接口
//	 * 同比：同比是上一年的比较   比如2021年9月22的数据 就是跟2020年9月22号进行比较
//	 * 参数1：比较的字段
//	 * return:返回对应的同比率
//	 * */
//	public  String  yearOnYear(String data){
//		String thisYearDate = DateUtils.getDate();
//		String lastYearDate=(Integer.parseInt(DateUtils.getYear())-1)+"-"+  DateUtils.getMonth()+"-"+DateUtils.getDay();
//		int a=  	query2(/*"CARD_COUNT"*/data,lastYearDate);
//		int b=      query2(/*"CARD_COUNT"*/data,thisYearDate);
//		//计算同比 同比增长率=（本期数－同期数）÷同期数×100%
//		float c= (float)(b-a)/(a);
//		log.info("同比a:"+a);
//		log.info("同比b:"+b);
//		log.info("同比c:"+c);
//		java.text.NumberFormat percentFormat =java.text.NumberFormat.getPercentInstance(); percentFormat.setMaximumFractionDigits(2); //最大小数位数
//		String  ret= percentFormat.format(c);//自动转换成百分比显示..
//		log.info("同比:"+ret);
//		return  ret;
//	}
//
//
//
//	/**
//	 *
//	 * 环比接口
//	 * 环比：2个周期的变化比  一般来说是拿月为单位, 但是不排除它拿周做比较
//	 * 参数1：比较的字段
//	 * */
//	public String   ringRatio(String data){
//		String currentCycleDate = DateUtils.getDate();
//		String lastCycleDate=DateUtils.getYear()+"-"+ (Integer.parseInt(DateUtils.getMonth())-1)+"-"+DateUtils.getDay();
//		int a=  	query2(/*"TRADE_COUNT"*/data,lastCycleDate);
//		int b=      query2(/*"TRADE_COUNT"*/data,currentCycleDate);
//		//计算环比  环比增长率=（本期的某个指标的值-上一期这个指标的值）/上一期这个指标的值*100%
//		float c= (float)(b-a)/a;
//		log.info("环比a:"+a);
//		log.info("环比b:"+b);
//		log.info("环比c:"+c);
//		java.text.NumberFormat percentFormat =java.text.NumberFormat.getPercentInstance(); percentFormat.setMaximumFractionDigits(2); //最大小数位数
//		String  ret= percentFormat.format(c);//自动转换成百分比显示..
//		log.info("环比:"+ret);
//		return  ret;
//
//	}
}
